/** @file res_handle_mgr.cpp
	@brief Resource handle manager */

#include "res/res_handle_mgr.h"

using namespace G3;

//==============================================================================
// Constructor
//==============================================================================
G3::RESOURCE_STATS::RESOURCE_STATS (time_t *aLastAccess, int *aLockCount, int aPriority, long aSize, uint aResID) {
	priority = aPriority;
	last_access = aLastAccess;
	lock_count = aLockCount;
	resource_id = aResID;
	size_in_memory = aSize;
	// Construction as the last access
	SetAccessed ();
}

//==============================================================================
// < operators
//==============================================================================
bool G3::RESOURCE_STATS::operator< (const RESOURCE_STATS &aStats) const {
	if (priority < aStats.priority)
		return true;
	else if (priority > aStats.priority)
		return false;
	else {
		if (last_access < aStats.last_access)
			return true;
		else if (last_access > aStats.last_access)
			return false;
		else {
			if (size_in_memory < aStats.size_in_memory)
				return true;
			else
				return false;
		}
	}
	return false;
}

//==============================================================================
// Sets the last access time
//==============================================================================
void G3::RESOURCE_STATS::SetAccessed () {
	Assert (last_access, "RESOURCE_STATS::SetAccessed: last_access member is a NULL pointer");
	*last_access = time (NULL);
}

//==============================================================================
// Is this resource locked?
//==============================================================================
bool G3::RESOURCE_STATS::IsLocked () const {
	Assert (lock_count, "RESOURCE_STATS::IsLocked: lock_count member is a NULL pointer");
	return (*lock_count > 0);
}
